﻿Imports Microsoft.SqlServer.Management.Smo
Imports Microsoft.SqlServer.Management.Common
Imports System.IO
Imports BUS

Public Class frmPhucHoi

    Private pathFolder As String
    Private fileBackupName As String
    Private dbName As String = "qlBanHangVBNet1"
    Private severName As String = ".\SQLEXPRESS"

    Private Sub btnThucHien_Click(sender As Object, e As EventArgs) Handles btnThucHien.Click
        
        If File.Exists(pathFolder) = False Then
            DevExpress.XtraEditors.XtraMessageBox.Show("File không tồn tại", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Else
            btnDong.Enabled = False
            btnThucHien.Enabled = False
            txtDuongDan.Properties.Buttons(0).Enabled = False
            Me.CloseBox = False

            Dim myServer As New Server(severName)
            Try

                myServer.ConnectionContext.LoginSecure = True
                myServer.ConnectionContext.Connect()
                Dim myDatabase As Database = myServer.Databases(dbName)
                RestoreDatabase(myServer, myDatabase)
            Catch ex As Exception
                DevExpress.XtraEditors.XtraMessageBox.Show(ex.InnerException.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
                btnDong.Enabled = True
                btnThucHien.Enabled = True
                txtDuongDan.Properties.Buttons(0).Enabled = True
                Me.CloseBox = True
            Finally
                If myServer.ConnectionContext.IsOpen Then
                    myServer.ConnectionContext.Disconnect()
                End If
            End Try
        End If
    End Sub

    Private Sub txtDuongDan_ButtonClick(sender As Object, e As DevExpress.XtraEditors.Controls.ButtonPressedEventArgs) Handles txtDuongDan.ButtonClick
        If e.Button.Tag.ToString = "btnOpenFolder" Then
            Dim dlg As New OpenFileDialog()
            dlg.Filter = "Backup file|*.bak"
            If DialogResult.OK = (New openFolder(dlg).Invoke()) Then
                txtDuongDan.Text = dlg.FileName
                pathFolder = dlg.FileName
            End If
        End If
    End Sub

    Private Sub btnDong_Click(sender As Object, e As EventArgs) Handles btnDong.Click
        Me.Close()
    End Sub

    Private Sub RestoreDatabase(myServer As Server, myDatabase As Database)

        Try
            HeThongBUS.dongKetNoiDatabase()

            Dim restoreDB As New Restore()
            restoreDB.Database = myDatabase.Name
            ' Specify whether you want to restore database or files or log etc 

            restoreDB.Action = RestoreActionType.Database
            restoreDB.Devices.AddDevice(pathFolder, DeviceType.File)
            restoreDB.ReplaceDatabase = True
            restoreDB.NoRecovery = False
            AddHandler restoreDB.PercentComplete, AddressOf CompletionStatusInPercent
            AddHandler restoreDB.Complete, AddressOf Backup_Completed
            restoreDB.SqlRestore(myServer)

        Catch ex As Exception
            DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error)
            proTienTrinh.Position = 100
            btnDong.Enabled = True
            btnThucHien.Enabled = True
            txtDuongDan.Properties.Buttons(0).Enabled = True
        End Try


    End Sub

    Private Sub CompletionStatusInPercent(sender As Object, args As PercentCompleteEventArgs)
        If proTienTrinh.Position < proTienTrinh.Properties.Maximum Then
            If (proTienTrinh.Position + args.Percent) <= 100 Then
                proTienTrinh.Position += args.Percent
            Else
                proTienTrinh.Position = 100
                btnDong.Enabled = True
                btnThucHien.Enabled = True
                txtDuongDan.Properties.Buttons(0).Enabled = True
                Me.CloseBox = True
            End If
        End If
    End Sub

    Private Sub Backup_Completed(sender As Object, args As ServerMessageEventArgs)
        HeThongBUS.themNhatKy(frmMain.maND, "Hệ Thống", "Phục Hồi Dữ Liệu", "")
        proTienTrinh.Position = 100
        btnDong.Enabled = True
        btnThucHien.Enabled = True
        txtDuongDan.Properties.Buttons(0).Enabled = True
        Me.CloseBox = True
        DevExpress.XtraEditors.XtraMessageBox.Show("Phục hồi dữ liệu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Private Sub frmPhucHoi_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        dbName = HeThongBUS.layDataBase()
        severName = HeThongBUS.layTenSever()
        proTienTrinh.Properties.PercentView = True
        proTienTrinh.Properties.Maximum = 100
        proTienTrinh.Properties.Minimum = 0
    End Sub
End Class